<?php

class UserCTR extends Controller
{
    private $config;
    function init()
    {
        $ConfigModel = new Model('config');
        $config = $ConfigModel->fetch();
        $this->config = $config;
        $this->assign('config', $config);
    }

    function login()
    {
        $tip = '';
        if (isset($_POST['submit'])) {
            $user = $_POST['user'];
            $pass = $_POST['pass'];
            if ($user && $pass) {
                $UserModel = new Model('user');
                $res = $UserModel->fetch('*', array('user' => $user, 'pass' => md5($pass)));
                if ($res) {
                    $UserModel->update(array('logintime' => time()), array('id' => $res['id']));
                    Session::set('user', $res);
                    $this->success('欢迎回来', $this->urlCreate('index', 'index'));
                    exit;
                } else {
                    $tip = '账号密码错误，请确认后再登录';
                }
            } else {
                $tip = '账号密码不能为空';
            }
        }
        $this->assign('title', '会员登录 - ' . $this->config['name']);
        $this->assign('register', $this->urlCreate('user', 'register'));
        $this->assign('tip', $tip);
        $this->display();
    }

    function register()
    {
        $tip = '';
        if (isset($_POST['submit'])) {
            $user = $_POST['user'];
            $pass = $_POST['pass'];
            $repass = $_POST['repass'];
            $verify = $_POST['verify'];
            if (strtolower($verify) == $_SESSION['verify']) {
                if ($pass == $repass) {
                    $UserModel = new Model('user');
                    if (!$UserModel->fetch('*', array('user' => $user))) {
                        if ($UserModel->insert(array(
                            'user' => $user,
                            'pass' => md5($pass),
                            'rtime' => time(),
                            'authlv' => '10'))) {
                            $this->success('注册成功', $this->urlCreate('user', 'login'));
                            exit;
                        } else {
                            $tip = '注册失败，请重试';
                        }
                    } else {
                        $tip = '该账号已经存在';
                    }
                } else {
                    $tip = '密码验证错误，请确认';
                }
            } else {
                $tip = '验证码错误';
            }
        }
        $this->assign('title', '注册账号 - ' . $this->config['name']);
        $this->assign('register', $this->urlCreate('user', 'login'));
        $this->assign('verify', $this->urlCreate('verify', 'index'));
        $this->assign('tip', $tip);
        $this->display();
    }

    function home()
    {
        $UserModel = new Model('user');
        if (isset($_GET['id']) && !empty($_GET['id'])) {
            $user_info = $UserModel->fetch('*', array('id' => ($_GET['id'] + 0)));
        } elseif (Session::get('user')) {
            $user = Session::get('user');
            $user_info = $UserModel->fetch('*', array('id' => $user['id']));
        } else {
            $this->error('非法操作', $this->urlCreate('index', 'index'));
            exit;
        }
        $this->assign('title', '会员中心 - ' . $this->config['name']);
        $this->assign('user_info', $user_info);
        $this->assign('login_user', Session::get('user'));
        $this->display();
    }

    function logout()
    {
        if (Session::get('user')) {
            Session::del('user');
            $this->success('注销成功', $this->urlCreate('index', 'index'));
        } else {
            $this->error('非法操作', $this->urlCreate('index', 'index'));
        }
    }
}

?>